﻿using System.Collections.Generic;
using System.Data;
using System.Linq;
using CL.Business.Entity;
using CL.Dapper.Repository;
using CL.Enum.Common;
using CL.View.Entity.Business;
using Dapper;

namespace CL.Business.DAL
{
    public class SettleMonthDAL : DataRepositoryBase<SettleMonthEntity>
    {
        public SettleMonthDAL(DbConnectionEnum conenum, IDbConnection Db = null) : base(conenum, Db)
        {

        }
        /// <summary>
        /// 结算中心
        /// </summary>
        /// <param name="AccountID"></param>
        /// <param name="currentYear">当前查看年份</param>
        /// <returns></returns>
        public List<SettleMonthEntity> Querydivided(int AccountID, string currentYear)
        {
            var Parms = new DynamicParameters();
            Parms.Add("@AccountID", AccountID);
            Parms.Add("@currentYear", currentYear + "%");
            string sql = " SELECT  [BuyMount], [Month], [RebateNum], [SettleAmount], [SettleStatus] FROM [dbo].[UN_SettleMonth] WHERE AccountID = @AccountID AND Month LIKE @currentYear ORDER BY SettleTime DESC";
            return new DataRepositoryBase<SettleMonthEntity>(DbConnectionEnum.CaileBusiness).QueryList(sql, Parms).ToList();
        }


        public List<udv_SettleMonth> QueryCommission(string AccountCode, string Month,int PageIndex, int PageSize, ref int RecordCount)
        {
            var Parms = new DynamicParameters();
            Parms.Add("@AccountName", AccountCode);
            Parms.Add("@SearchMonth", Month);
            Parms.Add("@PageIndex", PageIndex);
            Parms.Add("@PageSize", PageSize);
            Parms.Add("@RecordCount", RecordCount, DbType.Int32, ParameterDirection.Output);
            var Entitys =new DataRepositoryBase<udv_SettleMonth>(DbConnectionEnum.CaileBusiness).QueryList("udp_CommissionSettlement", Parms, CommandType.StoredProcedure).ToList();
            RecordCount = Parms.Get<int>("@RecordCount");
            return Entitys;
        }

        /// <summary>
        /// 批量插入对象
        /// </summary>
        /// <param name="Entity"></param>
        /// <returns></returns>
        public bool InsertSettleMonth(List<SettleMonthEntity> Entitys)
        {
            using (IDbTransaction tran = base.db.BeginTransaction())
            {
                try
                {
                    Entitys.ForEach((Entity) =>
                    {
                        base.Insert(Entity, tran);
                    });
                    tran.Commit();
                    return true;
                }
                catch
                {
                    tran.Rollback();
                    throw;
                }
            }
        }

        /// <summary>
        /// 更新对象
        /// </summary>
        /// <param name="Entity"></param>
        /// <returns></returns>
        public int ModifyEntity(SettleMonthEntity Entity)
        {
            return base.Update(Entity);
        }

        /// <summary>
        /// 查询用户对象
        /// </summary>
        /// <param name="AccountID"></param>
        /// <returns></returns>
        public SettleMonthEntity QueryEntity(int SettleMonthID)
        {
            return base.Get(SettleMonthID);
        }


    }
}
